﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HAILN_Y2016CS32ONLINE.Structure_Program.Exer
{
    public partial class calculator : Form
    {
        Double value=0;
        String tinhtoan = "";
        bool operation = false;
        public calculator()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, EventArgs e)
        {
            if ((txtResult.Text == "0") || (operation))
                txtResult.Clear();
            operation = false;
            Button b = (Button)sender;
            txtResult.Text = txtResult.Text + b.Text;

        }

        private void btnCE_Click(object sender, EventArgs e)
        {
            txtResult.Text = "0";
        }

        private void btnC_Click(object sender, EventArgs e)
        {
            txtResult.Text = "0";
            value = 0;
        }

        private void tinhtoan_click(object sender, EventArgs e)
        {
            label1.Text = "";
            Button b = (Button)sender;
            tinhtoan = b.Text;
            value = Double.Parse(txtResult.Text);
            operation = true;
            label1.Text = value + " " + tinhtoan;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            switch(tinhtoan)
            {
                case("+"):
                    txtResult.Text = (value + Double.Parse(txtResult.Text)).ToString();
                    break;
                case("-"):
                    txtResult.Text = (value- Double.Parse(txtResult.Text)).ToString();
                    break;
                case("*"):
                    txtResult.Text = (value* Double.Parse(txtResult.Text)).ToString();
                    break;
                case("/"):
                    txtResult.Text = (value/ Double.Parse(txtResult.Text)).ToString();
                    break;
                default:
                    break;            
            }
        }

        private void btnback_Click(object sender, EventArgs e)
        {
            txtResult.Text = txtResult.Text.Remove(txtResult.Text.Length-1,1);
        }        
    }
}
